from flask import Flask, render_template, request, redirect, url_for, session, Blueprint

from flaskr.utils.auth import query

bp = Blueprint('employee', __name__, url_prefix='/employee')


@bp.route('/', methods=['GET', 'POST'])
def employee():
    empID = session['empID']
    sql = "select * from employee where empID = '%s'" % empID
    result = query(sql)
    print(result)
    session['employee_info'] = result[0]
    return render_template('employee/employee_welcome.html', result=result)


# TODO: more
@bp.route('/check', methods=['GET', 'POST'])
def employee_check():
    empID = session['empID']
    sql = "select * from bills where user = '%s'" % empID
    result = query(sql)
    print(result)
    return render_template('employee/employee_check.html', result=result)


@bp.route('/add', methods=['GET', 'POST'])
def employee_add():
    if request.method == 'POST':
        amount = request.form.get('amount')
        reason = request.form.get('reason')
        time = request.form.get('time')
        print(amount)
        sql = "INSERT INTO bills (user, amount ,reason, time) " \
              "VALUES ('%s','%s', '%s', str_to_date('%s','%%Y-%%m-%%d %%T'))" \
              % (session['empID'], amount, reason, time)
        print(sql)
        query(sql)
        return render_template('employee/employee_add.html')

    else:
        return render_template('employee/employee_add.html')

